package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.ResumeMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.Resume;
import edu.hubu.talentmanagementsystem.service.IResumeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements IResumeService {

    private final MapperContext context;

    public ResumeServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getResumeMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<Resume> insert(@EncodeParam Resume record) {
        int insert = context.getResumeMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getResumeMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<Resume> selectById(Integer id) {
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Resume> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Resume::getResumeId, id);
        return Optional.of(context.getResumeMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<Resume> selectAll(String order) {
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Resume> select = getColumnWrapper(wrapper.lambda());
            return context.getResumeMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Resume> select = getColumnWrapper(wrapper.lambda());
        return context.getResumeMapper().selectList(select);
    }

    @Override
    public Page<Resume> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        Page<Resume> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Resume> select = getColumnWrapper(wrapper.lambda());
            return context.getResumeMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Resume> select = getColumnWrapper(wrapper.lambda());
        return context.getResumeMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<Resume> getColumnWrapper(LambdaQueryWrapper<Resume> wrapper) {
        return wrapper.select(
                Resume::getStaff
                    ,
                Resume::getResumeId
                    ,
                Resume::getResumeStaffId
                    ,
                Resume::getResumeUrl
        );
    }

    @Override
    public Page<Resume> selectLikePaged( Integer currentPage,
             Integer pageSize, Resume record, String order) {
        Page<Resume> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getResumeMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getResumeMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<Resume> getQueryWrapper(QueryWrapper<Resume>wrapper, Resume record) {
        LambdaQueryWrapper<Resume> lambda = wrapper.lambda();
        if (record.getStaff() != null) {
            lambda = lambda.eq(Resume::getStaff,record.getStaff());
        }
        if (record.getResumeId() != null) {
            lambda = lambda.eq(Resume::getResumeId,record.getResumeId());
        }
        if (record.getResumeStaffId() != null) {
            lambda = lambda.eq(Resume::getResumeStaffId,record.getResumeStaffId());
        }
        if (record.getResumeUrl() != null) {
            lambda = lambda.like(Resume::getResumeUrl,record.getResumeUrl ());
        }
        return lambda;
    }

    public int count(Resume record) {
        return context.getResumeMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<Resume> updateById(Integer id, @EncodeParam Resume record) {
        record.setResumeId(id);
        int update = context.getResumeMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<Resume> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<Resume> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Resume::getResumeId, id);
            return Optional.of(context.getResumeMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }

    @Override
    public Optional<Resume> selectByStaffId(Integer staffId) {
        Resume resume = context.getResumeMapper().selectOne(new QueryWrapper<Resume>().eq("resume_staff_id", staffId));
        return Optional.of(resume);
    }
}
